package com.hermit.system.controller;

import com.hermit.system.entity.BbsBookEntity;
import com.hermit.system.service.BbsBookService;
import com.hermit.util.ReqResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* 控制层
*/
@RestController
@RequestMapping("api/system/bbsBook")
public class BbsBookController {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private BbsBookService bbsBookService;

    /**
    * 查询所有
    */
    @RequestMapping("/query-all")
    public ReqResult queryAll() {
        logger.info(" BbsBook 查询所有数据");
        List<BbsBookEntity> list = bbsBookService.queryAll();
        return ReqResult.ok().put("list", list);
    }

    /**
    * 新增
    */
    @RequestMapping("/save")
    public ReqResult save(@RequestBody BbsBookEntity bbsBookEntity) {
        if (bbsBookEntity.getId() == null) {
            logger.info(" BbsBook 保存信息");
            bbsBookEntity.setCreatedTime(new Date());
            bbsBookEntity.setUpdatedTime(new Date());
            bbsBookEntity.setDeleteFlag(0);
            bbsBookService.save(bbsBookEntity);
        } else {
            logger.info(" BbsBook 修改信息");
            bbsBookEntity.setUpdatedTime(new Date());
            bbsBookService.updateById(bbsBookEntity);
        }
        return ReqResult.ok();
    }

    /**
    * 删除
    */
    @RequestMapping("/delete")
    public ReqResult delete(@RequestBody Map<String, Object> params) {
        logger.info("书籍表 根据id删除");
        int id = (Integer) params.get("id");
        bbsBookService.delete(id);
        return ReqResult.ok();
    }

    /**
     * 借书
     */
    @RequestMapping("/borrow")
    public ReqResult borrow(@RequestBody BbsBookEntity bbsBookEntity) {
        logger.info("书籍表 借书");
        String res = bbsBookService.borrow(bbsBookEntity);
        return ReqResult.ok().put("res", res);
    }

    /**
     * 借书
     */
    @RequestMapping("/return-book")
    public ReqResult returnBook(@RequestBody BbsBookEntity bbsBookEntity) {
        logger.info("书籍表 还书");
        String res = bbsBookService.returnBook(bbsBookEntity);
        return ReqResult.ok().put("res", res);
    }

    /**
     * 查询借书记录
     */
    @RequestMapping("/query-record")
    public ReqResult queryRecord(@RequestBody Map<String, Object> params) {
        logger.info("书籍表 查借书记录");
        int id = (Integer) params.get("id");
        List<BbsBookEntity> bbsBookEntities = bbsBookService.queryRecord(id);
        return ReqResult.ok().put("res", bbsBookEntities);
    }
}